//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.andromeda.androbench2;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class Ranking extends CustomActivity {
    private ArrayList<RankingData> alRanking;
    SQLiteDatabase db;
    private Ranking.GroupAdapter gaRanking;
    private HistoryDB historyDBHelper;
    Intent itRank;
    private ListView lvRanking;
    Context mContext;
    double maxRankValue;
    ProgressDialog progDialog;
    String rank_info = "http://www.androbench.org/db_androbench/rank_v5.php?type=";
    boolean send_success;
    String strUnit;
    int try_send;
    TextView tvRankingTitle;

    public Ranking() {
    }

    public void onCreate(Bundle var1) {
        super.onCreate(var1);
        this.setContentView(R.layout.ranking);
        this.historyDBHelper = new HistoryDB(this, (String)null, (CursorFactory)null);
        this.mContext = this.getApplicationContext();
        this.tvRankingTitle = (TextView)this.findViewById(R.id.tv_ranking_title);
        this.lvRanking = (ListView)this.findViewById(R.id.RankingListView);
        this.alRanking = new ArrayList();
        this.itRank = this.getIntent();
        String[] var2 = this.itRank.getExtras().getString("type").split("_");
        if (!this.itRank.getExtras().getString("type").equals("v_avg_sr") && !this.itRank.getExtras().getString("type").equals("v_avg_sw")) {
            if (!this.itRank.getExtras().getString("type").equals("v_avg_rr") && !this.itRank.getExtras().getString("type").equals("v_avg_rw")) {
                if (this.itRank.getExtras().getString("type").equals("v_avg_insert") || this.itRank.getExtras().getString("type").equals("v_avg_update") || this.itRank.getExtras().getString("type").equals("v_avg_delete")) {
                    this.strUnit = " QPS";
                    this.tvRankingTitle.setText("Ranking: " + var2[2].toUpperCase() + " (default option)");
                }
            } else {
                this.strUnit = " IOPS(4K)";
                this.tvRankingTitle.setText("Ranking: " + var2[2].toUpperCase());
            }
        } else {
            this.strUnit = " MB/s";
            this.tvRankingTitle.setText("Ranking: " + var2[2].toUpperCase());
        }

        this.rank_info = this.rank_info + this.itRank.getExtras().getString("type");
        this.progDialog = new ProgressDialog(this);
        this.progDialog.setMessage("Loading...");
        this.progDialog.setIndeterminate(true);
        this.progDialog.show();
        (new Ranking.AddRankingList()).execute(new Void[0]);
    }

    public void onDestroy() {
        super.onDestroy();
        this.historyDBHelper.close();
    }

    class AddRankingList extends AsyncTask<Void, Void, Void> {
        AddRankingList() {
        }

        protected Void doInBackground(Void... var1) {
            Ranking.this.try_send = 0;
            Ranking.this.send_success = false;

            Ranking var4;
            for(Ranking.this.maxRankValue = 0.0D; Ranking.this.try_send < 3 && !Ranking.this.send_success; ++var4.try_send) {
                label132: {
                    boolean var10001;
                    HttpURLConnection var5;
                    try {
                        URL var2 = new URL(Ranking.this.rank_info);
                        var5 = (HttpURLConnection)var2.openConnection();
                    } catch (Exception var21) {
                        var10001 = false;
                        break label132;
                    }

                    if (var5 != null) {
                        label143: {
                            label133: {
                                String[] var7;
                                try {
                                    var5.setConnectTimeout(10000);
                                    var5.setUseCaches(false);
                                    if (var5.getResponseCode() != 200) {
                                        break label133;
                                    }

                                    BufferedReader var6 = new BufferedReader(new InputStreamReader(var5.getInputStream()));
                                    if (!var6.readLine().equals("success")) {
                                        break label133;
                                    }

                                    Ranking.this.send_success = true;
                                    var7 = var6.readLine().split("##");
                                } catch (Exception var20) {
                                    var10001 = false;
                                    break label143;
                                }

                                int var8 = 0;

                                while(true) {
                                    String[] var9;
                                    try {
                                        if (var8 >= var7.length) {
                                            break;
                                        }

                                        var9 = var7[var8].split("~~");
                                    } catch (Exception var15) {
                                        var10001 = false;
                                        break label143;
                                    }

                                    if (var8 == 0) {
                                        label140: {
                                            label137: {
                                                try {
                                                    if (Ranking.this.itRank.getExtras().getDouble("result") >= Double.parseDouble(var9[1])) {
                                                        Ranking.this.maxRankValue = Ranking.this.itRank.getExtras().getDouble("result");
                                                        break label137;
                                                    }
                                                } catch (Exception var18) {
                                                    var10001 = false;
                                                    break label143;
                                                }

                                                try {
                                                    Ranking.this.maxRankValue = Double.parseDouble(var9[1]);
                                                } catch (Exception var14) {
                                                    var10001 = false;
                                                    break label143;
                                                }
                                            }

                                            Ranking.historyAVG var10;
                                            label138: {
                                                try {
                                                    var10 = Ranking.this.new historyAVG();
                                                    if (!Ranking.this.itRank.getExtras().getBoolean("flag_test")) {
                                                        break label138;
                                                    }

                                                    if (var10.cntHistory() > 0) {
                                                        Ranking.this.alRanking.add(new RankingData("-", "Your Device", Ranking.this.itRank.getExtras().getDouble("result") + " (Avg: " + var10.avgHistory() + ")" + Ranking.this.strUnit, Ranking.this.itRank.getExtras().getDouble("result"), Ranking.this.maxRankValue));
                                                        break label140;
                                                    }
                                                } catch (Exception var17) {
                                                    var10001 = false;
                                                    break label143;
                                                }

                                                try {
                                                    Ranking.this.alRanking.add(new RankingData("-", "Your Device", Ranking.this.itRank.getExtras().getDouble("result") + Ranking.this.strUnit, Ranking.this.itRank.getExtras().getDouble("result"), Ranking.this.maxRankValue));
                                                    break label140;
                                                } catch (Exception var13) {
                                                    var10001 = false;
                                                    break label143;
                                                }
                                            }

                                            try {
                                                if (var10.cntHistory() > 0) {
                                                    Ranking.this.alRanking.add(new RankingData("-", "Your Device", "Avg: " + var10.avgHistory() + Ranking.this.strUnit, Double.parseDouble(var10.avgHistory()), Ranking.this.maxRankValue));
                                                }
                                            } catch (Exception var16) {
                                                var10001 = false;
                                                break label143;
                                            }
                                        }
                                    }

                                    try {
                                        if (Ranking.this.maxRankValue < Double.parseDouble(var9[1])) {
                                            Ranking.this.maxRankValue = Double.parseDouble(var9[1]);
                                        }
                                    } catch (Exception var19) {
                                        var10001 = false;
                                        break label143;
                                    }

                                    try {
                                        Ranking.this.alRanking.add(new RankingData("" + (var8 + 1), var9[0], var9[1] + Ranking.this.strUnit, Double.parseDouble(var9[1]), Ranking.this.maxRankValue));
                                    } catch (Exception var12) {
                                        var10001 = false;
                                        break label143;
                                    }

                                    ++var8;
                                }
                            }

                            try {
                                var5.disconnect();
                            } catch (Exception var11) {
                                var10001 = false;
                            }
                        }
                    }
                }

                var4 = Ranking.this;
            }

            return null;
        }

        protected void onPostExecute(Void var1) {
            Ranking.this.progDialog.dismiss();
            if (Ranking.this.alRanking.size() > 0) {
                Ranking.this.gaRanking = Ranking.this.new GroupAdapter(Ranking.this.mContext, R.layout.row_rank, Ranking.this.alRanking);
                Ranking.this.lvRanking.setAdapter(Ranking.this.gaRanking);
            }

            if (!Ranking.this.send_success) {
                Toast.makeText(Ranking.this.mContext, "Connection failed : The ranking service needs to connect internet. Check your network connection", 1).show();
                if (Ranking.this.alRanking.size() == 0) {
                    Ranking.this.finish();
                }
            }

        }
    }

    private class GroupAdapter extends ArrayAdapter<Object> {
        private ArrayList<RankingData> item;
        private RankingData temp;

        public GroupAdapter(Context var2, int var3, ArrayList var4) {
            super(var2, var3, var4);
            this.item = var4;
        }

        public View getView(int var1, View var2, ViewGroup var3) {
            View var4 = var2;
            this.temp = (RankingData)this.item.get(var1);
            if (var2 == null) {
                var4 = ((LayoutInflater)Ranking.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.row_rank, (ViewGroup)null);
            }

            if (this.temp != null) {
                TextView var5 = (TextView)var4.findViewById(R.id.row_rank_title);
                var5.setText(this.temp.getTitle());
                TextView var6 = (TextView)var4.findViewById(R.id.row_rank_value);
                var6.setText(this.temp.getValue());
                TextView var7 = (TextView)var4.findViewById(R.id.row_rank_rank);
                var7.setText(this.temp.getRank());
                ProgressBar var8 = (ProgressBar)var4.findViewById(R.id.row_rank_progress);
                var8.setMax((int)(1000.0D * Ranking.this.maxRankValue));
                var8.setProgress(this.temp.getDValue());
                var5.setTypeface(Typeface.createFromAsset(Ranking.this.getApplicationContext().getAssets(), "fonts/PT_Sans-Web-Regular.ttf"));
                var6.setTypeface(Typeface.createFromAsset(Ranking.this.getApplicationContext().getAssets(), "fonts/PT_Sans-Web-Regular.ttf"));
                var7.setTypeface(Typeface.createFromAsset(Ranking.this.getApplicationContext().getAssets(), "fonts/PT_Sans-Web-Regular.ttf"));
            }

            return var4;
        }
    }

    class historyAVG {
        historyAVG() {
        }

        private String avgHistory() {
            String var1 = this.chkType(Ranking.this.itRank.getExtras().getString("type"));
            Ranking.this.db = Ranking.this.historyDBHelper.getReadableDatabase();
            Cursor var2;
            if (!var1.equals("avg_perf_sqlite_insert") && !var1.equals("avg_perf_sqlite_update") && !var1.equals("avg_perf_sqlite_delete")) {
                var2 = Ranking.this.db.rawQuery("SELECT " + var1 + " FROM history WHERE " + var1 + ">0;", (String[])null);
            } else {
                var2 = Ranking.this.db.rawQuery("SELECT " + var1 + " FROM history WHERE " + var1 + ">0 and transaction_size = 1 and journal_mode = 'WAL' and index_usage = 1;", (String[])null);
            }

            double var3;
            for(var3 = 0.0D; var2.moveToNext(); var3 += var2.getDouble(0)) {
            }

            double var5 = (double)((int)(100.0D * (var3 / (double)var2.getCount()))) / 100.0D;
            var2.close();
            Ranking.this.db.close();
            return "" + var5;
        }

        private String chkType(String var1) {
            String var2 = "unknown";
            if (var1.equals("v_avg_sr")) {
                var2 = "avg_mbps_sr";
            } else {
                if (var1.equals("v_avg_sw")) {
                    return "avg_mbps_sw";
                }

                if (var1.equals("v_avg_rr")) {
                    return "avg_iops_rr";
                }

                if (var1.equals("v_avg_rw")) {
                    return "avg_iops_rw";
                }

                if (var1.equals("v_avg_insert")) {
                    return "avg_perf_sqlite_insert";
                }

                if (var1.equals("v_avg_update")) {
                    return "avg_perf_sqlite_update";
                }

                if (var1.equals("v_avg_delete")) {
                    return "avg_perf_sqlite_delete";
                }
            }

            return var2;
        }

        private int cntHistory() {
            String var1 = this.chkType(Ranking.this.itRank.getExtras().getString("type"));
            Ranking.this.db = Ranking.this.historyDBHelper.getReadableDatabase();
            Cursor var2 = Ranking.this.db.rawQuery("SELECT * FROM history WHERE " + var1 + ">0;", (String[])null);
            int var3 = var2.getCount();
            var2.close();
            Ranking.this.db.close();
            return var3;
        }
    }
}
